package com.demo.core.verification;

import cn.hutool.core.util.ObjectUtil;
import com.demo.core.enums.ErrorEnum;
import com.demo.core.enums.OftenEnum;
import com.demo.core.exception.ServerException;
import com.demo.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ObjectUtils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author Tan-Bowen
 * @version 1.0
 * @description TODO
 * @date 2021/7/29 11:18
 */
@Slf4j
public class ValidatorTools {

    /**
     * @return boolean
     * @author Tan-Bowen
     * @description TODO 判断对象中某个值是否相同
     * @date 2021/9/22 20:15
     * @paramType [java.util.function.Function<T, E>, T, E]
     */
    public static <T, E> boolean checkAttribute(Function<T, E> fuc, T t, E e) {
        if (t == null) {
            return false;
        }
        return ObjectUtil.equal(fuc.apply(t), e);
    }


    /**
     * @author Tan-Bowen
     * @description TODO 公用抛出数据库操作失败提示
     * @date 2021/9/22 18:07
     */
    public static void checkSqlSuccess(int count, String message) {
        if (count <= 0) {
            throw new ServerException(ErrorEnum.SQL_HANDLE_ERROR, message);
        }
    }

    /**
     * @author Tan-Bowen
     * @description TODO 公用抛出错误提示
     * @date 2021/9/22 18:07
     */
    public static void commonThrow(boolean t, String msg) {
        if (t) {
            throw new ServerException(ErrorEnum.SERVER_HANDLE_ERROR, msg);
        }
    }

    public static void isNullThrow(List<?> list, String msg) {
        commonThrow(isEmpty(list), msg);
    }

    public static <T> void isNullThrow(T t, String msg) {
        commonThrow(t == null || StringUtils.isBlank(t.toString()), msg);
    }

    public static <T, E> void isNullThrow(T t, Function<T, E> fuc, String msg) {
        E v = fuc.apply(t);
        commonThrow(t == null || v == null || StringUtils.isBlank(v.toString()), msg);
    }

    public static <T, E> void delThrow(T t, Function<T, E> fuc, String msg) {
        commonThrow(t == null || StringUtils.isBlank(t.toString()) || ObjectUtil.equal(fuc.apply(t), OftenEnum.DelEnum.YES_DEL.code()), msg);
    }

    /**
     * @author Tan-Bowen
     * @description TODO 是否是超管
     * @date 2021/9/22 18:06
     */
    public static Boolean isSuper(Long deptId) {
        return ObjectUtil.equal(OftenEnum.Super.SUPER_MAN.code().toString(), deptId.toString());
    }

    /**
     * @author Tan-Bowen
     * @description TODO 判空
     * @date 2021/9/22 18:09
     */
    public static boolean notEmpty(List<?> list) {
        return list != null && !list.isEmpty();
    }

    /**
     * @author Tan-Bowen
     * @description TODO 判空
     * @date 2021/9/23 10:14
     */
    public static boolean isEmpty(List<?> list) {
        return list == null || list.isEmpty();
    }


    public static boolean notBlankStrings(String... args) {
        for (String arg : args) {
            if (StringUtils.isBlank(arg)) {
                return false;
            }
        }
        return true;
    }

    public static String encryption(String code) {
        return base64Encoder(md5Byte(code));
    }

    public static byte[] md5Byte(String code) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            return md5.digest(code.getBytes(StandardCharsets.UTF_8));
        } catch (Exception e) {
            e.printStackTrace();
            log.info("加密失敗");
        }
        throw new ServerException(ErrorEnum.SERVER_HANDLE_ERROR, "加密失敗");
    }

    public static String base64Encoder(byte[] code) {
        Base64.Encoder base64 = Base64.getEncoder();
        return base64.encodeToString(code);
    }

    public static boolean decrypt(String code, String code1) {
        String newCode = encryption(code1);
        return ObjectUtils.equals(code, newCode);
    }

    public static void main(String[] args) {
        List<User> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            User user = User.builder().name(i + "测试").sex(i).nickName(i + "").logo(i + "").remarks(i + "").state(i).build();
            list.add(user);
        }
        Map<Object, Long> res = list.stream().collect(Collectors.groupingBy(e -> e.getName() + e.getNickName(), Collectors.counting()));
        System.out.println(res);
        Set<String> projectIds = list.stream().reduce(new HashSet<>(), (a, b) -> {
            a.add(b.getName());
            return a;
        }, (c, d) -> {
            return c;
        }); ///2bHX20zW5wk1Nooe+xDjdw==
        log.info("::::::::{}", encryption("202cb962ac59075b964b07152d234b70"));
    }

}
